<template>
  <div class="box">
    <h2>Son2 子组件</h2>
    从vuex中获取值：<span>{{ count }}</span>
    <br/>
    <button @click="removeCount(1)">值 - 1</button>
    <button @click="removeCount(5)">值 - 5</button>
    <button @click="removeCountAction(5)">2s后-5</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex'

export default {
  name: 'Son2Com',
  computed: {
    // 以下2种方式都可以获取store中的值
    // count () {
    //   return this.$store.state.count
    // }
    ...mapState(['count'])
  },

  // 以下2种方式都可以修改store中的值
  methods: {
    ...mapMutations(['removeCount']),
    // removeCount (n) {
    //   this.$store.commit('removeCount', n)
    // }

    // 延迟修改store中的值
    // removeCountAction (count) {
    //   this.$store.dispatch('removeCountAction', count)
    // }
    ...mapActions(['removeCountAction'])
  }
}
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}

h2 {
  margin-top: 10px;
}
</style>
